package is.progetto2.gui;

import is.progetto2.activity.Activity;
import is.progetto2.activity.Bind;
import is.progetto2.activity.BindType;
import is.progetto2.activity.Job;

import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class BindCreator {

	// ----------------------------LOGICHE--------------
	private Activity activity;
	private Bind bind;
	// ----------------------------GRAFICHE-------------
	private JFrame frame;
	private JLabel lblLavoro;
	private JLabel lblTipo;
	private JLabel lblVincolo;
	private JLabel lblVincolo_1;
	private JLabel lblLavoro_1;
	private JComboBox<BindType> comboBoxVincolo1;
	private JComboBox<Job> comboBoxLavoro1;
	private JComboBox<BindType> comboBoxVincolo2;
	private JComboBox<BindType> comboBoxVincolo3;
	private JComboBox<Job> comboBoxLavoro2;
	private JButton btnOk;
	private JButton btnAnnulla;
	private Component parent;
	private Component refresh;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BindCreator window = new BindCreator();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public BindCreator() {
		initialize();
	}

	public BindCreator(Component c, Activity a, Component refresh) {
		this.activity = a;
		initialize();
		this.parent = c;
		frame.setVisible(true);
		frame.setTitle("Crea vincolo");
		this.refresh=refresh;
		if (parent != null) {
			parent.setEnabled(false);
		}
	}

	public BindCreator(Component c, Activity a, Bind b, Component refresh) {
		this.activity = a;
		this.bind = b;
		initialize();
		this.parent = c;
		frame.setTitle("Modifica vincolo");
		frame.setVisible(true);
		this.refresh=refresh;
		if (parent != null) {
			parent.setEnabled(false);
		}
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				if (parent != null) {
					parent.setEnabled(true);
				}
				frame.dispose();
			}
		});
		frame.setBounds(100, 100, 285, 177);
		frame.getContentPane().setLayout(null);
		frame.getContentPane().add(getLblLavoro());
		frame.getContentPane().add(getLblTipo());
		frame.getContentPane().add(getLblVincolo());
		frame.getContentPane().add(getLblVincolo_1());
		frame.getContentPane().add(getLblLavoro_1());
		frame.getContentPane().add(getComboBoxVincolo1());
		frame.getContentPane().add(getComboBoxLavoro1());
		frame.getContentPane().add(getComboBoxVincolo2());
		frame.getContentPane().add(getComboBoxVincolo3());
		frame.getContentPane().add(getComboBoxLavoro2());
		frame.getContentPane().add(getBtnOk());
		frame.getContentPane().add(getBtnAnnulla());
	}

	private JLabel getLblLavoro() {
		if (lblLavoro == null) {
			lblLavoro = new JLabel("Vincolo1");
			lblLavoro.setBounds(6, 6, 55, 16);
		}
		return lblLavoro;
	}

	private JLabel getLblTipo() {
		if (lblTipo == null) {
			lblTipo = new JLabel("Lavoro1:");
			lblTipo.setBounds(6, 34, 55, 16);
		}
		return lblTipo;
	}

	private JLabel getLblVincolo() {
		if (lblVincolo == null) {
			lblVincolo = new JLabel("Vincolo2:");
			lblVincolo.setBounds(6, 62, 55, 16);
		}
		return lblVincolo;
	}

	private JLabel getLblVincolo_1() {
		if (lblVincolo_1 == null) {
			lblVincolo_1 = new JLabel("Vincolo3:");
			lblVincolo_1.setBounds(6, 90, 55, 16);
		}
		return lblVincolo_1;
	}

	private JLabel getLblLavoro_1() {
		if (lblLavoro_1 == null) {
			lblLavoro_1 = new JLabel("Lavoro2:");
			lblLavoro_1.setBounds(6, 118, 55, 16);
		}
		return lblLavoro_1;
	}

	private JComboBox<BindType> getComboBoxVincolo1() {
		if (comboBoxVincolo1 == null) {
			if (activity != null) {
				comboBoxVincolo1 = new JComboBox<BindType>(
						new BindType1ListModel());
				if (bind != null) {
					comboBoxVincolo1.setSelectedItem(bind.getBind1());
				}
			} else {
				comboBoxVincolo1 = new JComboBox<BindType>();
			}
			comboBoxVincolo1.setBounds(73, 1, 90, 26);
		}
		return comboBoxVincolo1;
	}

	private JComboBox<Job> getComboBoxLavoro1() {
		if (comboBoxLavoro1 == null) {
			if (activity != null) {
				comboBoxLavoro1 = new JComboBox<Job>(new JobListModel(activity));
				if (bind != null) {
					comboBoxLavoro1.setSelectedItem(bind.getJob1());
					comboBoxLavoro1.setEnabled(false);
				}
			} else {
				comboBoxLavoro1 = new JComboBox<Job>();
			}
			comboBoxLavoro1.setBounds(73, 29, 90, 26);
		}
		return comboBoxLavoro1;
	}

	private JComboBox<BindType> getComboBoxVincolo2() {
		if (comboBoxVincolo2 == null) {
			if (activity != null) {
				comboBoxVincolo2 = new JComboBox<BindType>(
						new BindType2ListModel());
				if (bind != null) {
					comboBoxVincolo2.setSelectedItem(bind.getBind2());
				}
			} else {
				comboBoxVincolo2 = new JComboBox<BindType>();
			}
			comboBoxVincolo2.setBounds(73, 57, 90, 26);
		}
		return comboBoxVincolo2;
	}

	private JComboBox<BindType> getComboBoxVincolo3() {
		if (comboBoxVincolo3 == null) {
			if (activity != null) {
				comboBoxVincolo3 = new JComboBox<BindType>(
						new BindType1ListModel());
				if (bind != null) {
					comboBoxVincolo3.setSelectedItem(bind.getBind3());
				}
			} else {
				comboBoxVincolo3 = new JComboBox<BindType>();
			}
			comboBoxVincolo3.setBounds(73, 85, 90, 26);
		}
		return comboBoxVincolo3;
	}

	private JComboBox<Job> getComboBoxLavoro2() {
		if (comboBoxLavoro2 == null) {
			if (activity != null) {
				comboBoxLavoro2 = new JComboBox<Job>(new JobListModel(activity));
				if (bind != null) {
					comboBoxLavoro2.setSelectedItem(bind.getJob2());
				}
			} else {
				comboBoxLavoro2 = new JComboBox<Job>();
			}
			comboBoxLavoro2.setBounds(73, 113, 90, 26);
		}
		return comboBoxLavoro2;
	}

	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton("Ok");
			btnOk.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					BindType bindtype1 = (BindType) comboBoxVincolo1
							.getSelectedItem();
					BindType bindtype2 = (BindType) comboBoxVincolo2
							.getSelectedItem();
					BindType bindtype3 = (BindType) comboBoxVincolo3
							.getSelectedItem();
					Job job1 = (Job) comboBoxLavoro1.getSelectedItem();
					Job job2 = (Job) comboBoxLavoro2.getSelectedItem();
					if (bindtype1 != null && bindtype2 != null
							&& bindtype3 != null && job1 != null
							&& job2 != null) {
						if (activity != null) {
							if (bind != null) {
								bind.setBind1(bindtype1);
								bind.setBind2(bindtype2);
								bind.setBind3(bindtype3);
								bind.setJob2(job2);
							} else {
								if (job1.equals(job2)) {
									JOptionPane.showMessageDialog(frame,
											"Selezionare lavori differenti",
											"I lavori immessi sono uguali",
											JOptionPane.ERROR_MESSAGE);
								} else {
									activity.createBind(bindtype1,job1,bindtype2,bindtype3,job2);
								}
							}
							if (parent != null) {
								parent.setEnabled(true);
							}
							if(refresh!=null){
								if(refresh instanceof JTable){
									JTable ref=(JTable)refresh;
									ref.setModel(new BindTableModel(activity));
								}
							}
							frame.dispose();
						}
					} else {
						JOptionPane.showMessageDialog(frame,
								"Seleziona non valida",
								"Non hai selezionato tutti i camopi",
								JOptionPane.ERROR_MESSAGE);
					}
				}
			});
			btnOk.setBounds(173, 34, 90, 28);
		}
		return btnOk;
	}

	private JButton getBtnAnnulla() {
		if (btnAnnulla == null) {
			btnAnnulla = new JButton("Annulla");
			btnAnnulla.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (parent != null) {
						parent.setEnabled(true);
					}
					frame.dispose();
				}
			});
			btnAnnulla.setBounds(173, 78, 90, 28);
		}
		return btnAnnulla;
	}
}
